function buscarModeloP(){
    if($("#buscar").val().length===0){
        return;
    }
    $.ajax(
    {
        type: "GET",
        url: "buscarModelo2",
        async: false,
        dataType: "text",
        data: "buscar="+$("#buscar").val(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#body-content").html(transport);
            $('input[id="btnVer"]').click(verModeloP);
            $('input[id="btnEliminar"]').click(eliminarModeloP);
            $('input[id="btnModificar"]').click(modificarModeloP);
            $('#btnAgregar').click(agregarModeloP);
            $('#btnBuscar').click(buscarModeloP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function modificarModeloAP(){
    if(!validaAltaModeloP()){
        alert("Campos invalidos");
        return false;
    }
        
    $.ajax(
    {
        type: "POST",
        url: "modificarModeloP",
        async: false,
        dataType: "text",
        data: $('#modficarModeloF').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuModelo').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuModelo').trigger('click');
    $('#menuModelo').trigger('click');
    return false;                        
}

function modificarModeloP(){    
    $("#tablaModelo").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "gestionModificarModeloPreP",
        async: false,
        dataType: "text",
        data: "idModelo="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verModelo").html(transport);
            $(".error-inner").hide();
            $("#verModelo").show();
            $("#tablaModelo").hide();
            $('#btnModificarModelo').click(modificarModeloAP);
            $("#btnRegresar").click(mostrarModeloP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function eliminarModeloP(){
     id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar el modelo "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarModeloP",
        async: false,
        dataType: "text",
        data: "idModelo="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function verModeloP(){   
    $.ajax(
    {
        type: "GET",
        url: "consultaModeloP",
        async: false,
        dataType: "text",
        data: "idModelo="+ $(this).closest('tr').attr("id"),       
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verModelo").html(transport);
            $("#verModelo").show();
            $("#tablaModelo").hide();
            $("#btnRegresar").click(mostrarModeloP);
				
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function mostrarModeloP(){
    //alert("hola");
    $("#verModelo").hide();   
    $("#tablaModelo").show();
}

function agregarModeloP(){ 
    //alert("hola");
    $("#tablaModelo").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "altaModeloPreP",
        async: false,
        dataType: "text",
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verModelo").html(transport);
            $(".error-inner").hide();
            $("#verModelo").show();
            $("#tablaModelo").hide();
            $('#btnAltaModelo').click(altaModeloP);
            $("#btnRegresar").click(mostrarModeloP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function altaModeloP(){
    //alert("hola");
    console.log("dfdfdfdf");
    console.log("hola");
    console.log($('#altaModelo').serialize() + "hola");
    
    if(!validaAltaModeloP())
        return false;
       
    $.ajax(
    {
        type: "POST",
        url: "altaModeloP",
        async: false,
        dataType: "text",
        data: $('#altaModelo').serialize(),
        beforeSend: function(){
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuModelo').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuModelo').trigger('click');
    $('#menuModelo').trigger('click');
    return false;
}

function validaAltaModeloP(){
    $('.error-inner').hide();
    esValido = true;
    if($('#modelo').val().length<3){
        $("#emodelo").show();
        esValido = false;
    }
    if($('#descripcion').val().length<3){
        $("#edescripcion").show();
        esValido = false;
    }    
    return esValido;
}

